layui.define(function (exports) {


    // 基础工具类
    var kit = {
        tips: {
            ok: function (_message) {
                swal(_message, '', 'success');
            },
            error: function (_message) {
                swal(_message, '', 'error');
            },
            question: function (_message) {
                swal(_message, '');
            },
            warning: function (_message) {
                swal(_message, '', 'warning');
            },
            confirm: function (_config) {
                swal({
                    title: '操作提醒',
                    text: (typeof _config.text) == "undefined" ? '你将无法恢复它！' : _config.text,
                    type: 'question',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: (typeof _config.btn) == "undefined" ? '确 定' : _config.confirm,
                    cancelButtonText: '放 弃',
                    confirmButtonClass: 'btn btn-success',
                    cancelButtonClass: 'btn btn-danger',
                    buttonsStyling: false
                }).then(function (e) {
                    if (e.value) {
                        _config.confirm ? _config.confirm() : '';
                    }
                }, function (dismiss) {

                })
            }
        },
        http: {
            get: function (request) {
                request.method = 'get';
                return ajax.send(request);
            },
            post: function (request) {
                request.method = 'post';
                return ajax.send(request);
            },
            put: function (request) {
                request.method = 'put';
                return ajax.send(request);
            },
            delete: function (request) {
                request.method = 'delete';
                return ajax.send(request);
            }
        },
        array: {
            // 合并多个数组
            coalition: function () {
                var arrs = arguments;
                var tem = [];
                $.each(arrs, function (index, obj) {
                    tem = tem.concat(obj)
                })
                return tem;
            }
        },
        json: {
            toString: function (_obj) {
                return JSON.stringify(_obj);
            },
            toObject: function (_obj) {
                return JSON.parse(_obj);
            },
            remove: function (_obj, _key, _filed) {
                var tems = [];
                $.each(_obj, function (idx, obj) {
                    var $id = (typeof _filed) == "undefined" ? 'id' : _filed;
                    if (obj[$id] != _key) {
                        tems.push(obj);
                    }
                })
                return tems;
            },
            get: function (_obj, _val, _filed) {
                var tem = {};
                $.each(_obj, function (idx, obj) {
                    var $id = (typeof _filed) == "undefined" ? 'id' : _filed;
                    if (obj[$id] == _val) {
                        tem = obj;
                    }
                })
                return tem;
            },
            toForm: function (_form) {
                for (let v in _form) {
                    if ((typeof _form[v]) == 'number') {
                        _form[v] = _form[v].toString();
                    }
                }
                return _form;
            },
            getVal: function (_list, _filed) {
                let tem = [];
                $.each(_list, function (idx, obj) {
                    var $id = (typeof _filed) == "undefined" ? 'id' : _filed;
                    tem.push(obj[$id])
                })
                return tem;
            },
            tree2list: function(_tree, _key){
                return _tree.reduce(function(con, item) {
                    var callee = arguments.callee;
                    con.push(item);
                    if (item[_key] && item[_key].length >0){
                        item[_key].reduce(callee, con);
                    }
                    return con;
                }, [])
                .map(function(item){
                    item[_key] = [];
                    return item;
                })
            },
            list2tree: function(_list, _key){
                var tree = _list.filter(function(parent) {
                    var branchArr = _list.filter(function(child) {
                        return parent.id == child[_key];
                    });
                     parent.nodes = [];
                    if (branchArr.length > 0) {
                        parent.nodes = branchArr;
                    }
                    return parent[_key] == "";
                });
                return tree;
            }
        },
        session: {
            get: function (_key) {
                return layui.data(cache.platform)[_key];
            },
            put: function (_key, _val) {
                layui.data(cache.platform, {
                    key: _key,
                    value: _val
                });
            },
            delete: function (_key) {
                layui.data(cache.platform, {
                    key: _key,
                    remove: true
                });
            },
            getToken: function () {
                return kit.session.get(cache.token);
            },
            putToken: function (_val) {
                return kit.session.put(cache.token, _val);
            },
            deleteToken: function () {
                return kit.session.delete(cache.token);
            },
            getUser: function () {
                return kit.session.get(cache.user);
            },
            putUser: function (_val) {
                return kit.session.put(cache.user, _val);
            },
            deleteUser: function () {
                return kit.session.delete(cache.user);
            },
            clean: function () {
                layui.data(cache.platform, null);
            }
        },
        progress: {
            open: function () {
                layer.load(1, {scrollbar: false, shade: [0.5, '#393D49']});
            },
            close: function () {
                layer.closeAll('loading');
            }
        },
        form: {
            verify: function (_data, _form_id, _filed) {
                let v = {check: true, message: ''};
                let form_id = '#' + _form_id;
                $.each(_filed, function (index, filed) {
                    if ($.trim(_data[filed]).length == 0) {
                        v.check = false;
                        v.message = $(form_id).find('input[name="' + filed + '"]').attr('placeholder');
                        return false;
                    }
                })
                return v;
            },
            render: function (_form) {
                var emp = _form.form[0]
                if (Array.isArray(emp)) {
                    var wClass = 'layui-grid';
                    for (var i = 0; i < _form.form.length; i++) {
                        for (var j = 0; j < _form.form[i].length; j++) {
                            _form.form[i][j].wClass = wClass + '-' + _form.form[i].length
                        }
                    }
                } else if (emp.tabName) {
                    emp.tabClass = 'layui-this'
                    emp.showClass = 'layui-show'
                    for (var i = 1; i < _form.form.length; i++) {
                        _form.form[i].tabClass = '';
                        _form.form[i].showClass = ''
                    }
                }
                var parent = _form.parentId ? _form.parentId : "common-form-view"
                $("#" + parent).vm(_form);
                kit.form.initFormEvent(_form.form)
                form.render();
            },
            initFormEvent: function (_form) {
                var type = Array.isArray(_form[0]) ? 2 : (_form[0].tab ? 3 : 0)
                if (type == 0) {
                    for (var i = 0; i < _form.length; i++) {
                        if (_form[i].event) {
                            initEvt(_form[i])
                        }
                    }
                }
                else if (type == 2) {
                    for (var i = 0; i < _form.length; i++) {
                        for (var k = 0; k < _form[i].length; k++) {
                            if (_form[i][k].event) {
                                initEvt(_form[i][k])
                            }
                        }
                    }
                }
                else if (type == 3) {
                    for (var i = 0; i < _form.length; i++) {
                        for (var k = 0; k < _form[i].tab.length; k++) {
                            if (_form[i].tab[k].event) {
                                initEvt(_form[i].tab[k])
                            }
                        }
                    }
                }
                form.render();

                function initEvt(obj) {
                    var eventType = ''
                    if (obj.code == 2) {
                        eventType = 'radio(' + obj.field + ')'
                    }
                    form.on(eventType, function (data) {
                        obj.event(data)
                    });
                }
            },
            get: function (_form_id) {
                var d = {};
                let form_id = _form_id.indexOf('#') >= 0 ? _form_id : '#' + _form_id;
                var postData = $(form_id).serializeArray();
                $.each(postData, function () {
                    d[this.name] = this.value;
                });
                return d;
            },
            set: function (_id, _form) {
                form.val(_id, kit.json.toForm(_form))
            },
            checkbox: function (_name) {
                var cb = new Array();
                $('input[name="' + _name + '"]:checked').each(function () {
                    cb.push($(this).val());//向数组中添加元素
                });
                return cb;
            },
            edit: {
                get: function (_edit) {
                    return layui.layedit.getContent(_edit);
                },
                set: function (_edit, _content) {
                    return layui.layedit.setContent(_edit, _content, false);
                },
                init: function (_id, _array) {
                    let base = {
                        tool: [
                            'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'fontfamily', 'fontSize', 'fontBackColor', 'colorpicker', 'face',
                            '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'attachment', 'anchors', '|', 'table', 'fullScreen', 'image_alt'
                        ]
                    };
                    $.extend(base, _array);
                    layedit.set(base);
                    var editIndex = layedit.build(_id);
                    return editIndex;
                }
            },
            downLoad: function (obj) {
                var src = $(obj).attr("href");
                var link = document.createElement('a');
                link.href = src
                link.download = 'aaa';
                link.click();
            },
            //附加信息中取得某项的值
            getSingItemValue: function (code, filed) {
                var value;
                var emp = $("#" + filed);
                if (code == 12) {
                    var list = emp.find(".content .line")
                    var arr = [];
                    for (var i = 0, len = list.length; i < len; i++) {
                        var items = $(list[i]).find('.item')
                        var obj = {}
                        obj["opt"] = 1;
                        for (var j = 0, len2 = items.length; j < len2; j++) {
                            var type = $(items[j]).attr("data-type");
                            if (type) {
                                obj[type + "\""] = $(items[j]).html()
                            }
                        }
                        arr.push(obj);
                    }
                    value = arr;
                }
                return value
            },
            extendAdd: function (obj) {
                var pNode = $(obj).parent();
                var key = $($(pNode).find('[name="key"]')[0]).val();
                var value = $($(pNode).find('[name="val"]')[0]).val();
                var str = ' <div class="line">' +
                    '     <div class="item" data-type="key">' + key + '</div>' +
                    '     <div class="item" data-type="val">' + value + '</div>' +
                    '     <div class="item" style="color:red;cursor:pointer;" onclick="javascript:$($(this).parent()).remove();">删除</div>' +
                    ' </div>'
                var sibNode = $(pNode).next();
                $($(sibNode).find(".content")).append(str)
                $($(obj).parent().parent()).toggleClass('setting-hide');
                $($(pNode).find('[name="key"]')[0]).val('')
                $($(pNode).find('[name="val"]')[0]).val('')
            },
           
            //设置表单中下拉列表的值
            setFormItems: function (form, dicts) {
                var type = Array.isArray(form[0]) ? 2 : (form[0].tab ? 3 : 0)
                if (type == 0) {
                    for (var i = 0; i < form.length; i++) {
                        for (var j = 0; j < dicts.length; j++) {
                            if (dicts[j].code == form[i].dictCode) {
                                form[i].items = dicts[j].list
                                break;
                            }
                        }
                    }
                }
                else if (type == 2) {
                    for (var i = 0; i < form.length; i++) {
                        for (var k = 0; k < form[i].length; k++) {
                            for (var j = 0; j < dicts.length; j++) {
                                if (dicts[j].code == form[i][k].dictCode) {
                                    form[i][k].items = dicts[j].list
                                    break;
                                }
                            }
                        }
                    }
                }
                else if (type == 3) {
                    for (var i = 0; i < form.length; i++) {
                        for (var k = 0; k < form[i].tab.length; k++) {
                            for (var j = 0; j < dicts.length; j++) {
                                if (dicts[j].code == form[i].tab[k].dictCode) {
                                    form[i].tab[k].items = dicts[j].list
                                    break;
                                }
                            }
                        }
                    }
                }
            },
            getItemByField: function (field, _form) {
                var type = Array.isArray(_form[0]) ? 2 : (_form[0].tab ? 3 : 0)
                if (type == 0) {
                    for (var i = 0; i < _form.length; i++) {
                        if (_form[i].field == field) {
                            return _form[i]
                        }
                    }
                }
                else if (type == 2) {
                    for (var i = 0; i < _form.length; i++) {
                        for (var k = 0; k < _form[i].length; k++) {
                            if (_form[i][k].field == field) {
                                return (_form[i][k])
                            }
                        }
                    }
                }
                else if (type == 3) {
                    for (var i = 0; i < _form.length; i++) {
                        for (var k = 0; k < _form[i].tab.length; k++) {
                            if (_form[i].tab[k].field == field) {
                                return (_form[i].tab[k])
                            }
                        }
                    }
                }
            }
        },
        table: {
            render: function (_config, _resize) {
                // 双击配置项
                let info = _config.info;
                let tool = _config.tool;
                let bar = _config.bar;
                let radio = _config.radio;
                let tem = _config;
                let table_id = tem.id;
                let height = (typeof _config.height) == "undefined" ? 'full-150' : _config.height;
                let click = _config.click;
                if(_resize){
                    let h1 = $('#layui-body').height();
                    let h2 = $('#body-content-header').height();
                    height = (h1 - h2 - 24) + 'px';
                }
                if (_config.token) {
                    header.token = _config.token
                }
                let base = {
                    elem: '#' + tem.id,
                    defaultToolbar: [],
                    height: height,
                    headers: header,
                    url: action + tem.url,
                    cols: [tem.cols],
                    limits: [6, 12, 24, 36, 48],
                    limit: 12,
                    page: (typeof tem.page) == "undefined" ? true : tem.page,
                    where: (typeof tem.where) == "undefined" ? {} : tem.where,
                    done: (typeof tem.done) == "undefined" ? function () {
                    } : tem.done
                }
                delete tem.id;
                delete tem.url;
                delete tem.cols;
                delete tem.where;

                $.extend(base, tem);
                let baseTable = table.render(base);
                // 双击详情
                if (info) {
                    let rowDouble = 'rowDouble(' + table_id + ')';
                    table.on(rowDouble, function (obj) {
                        let data = obj.data;
                        app.info({
                            title: '',
                            height: (typeof info.height) == "undefined" ? '80%' : info.height,
                            width: (typeof info.width) == "undefined" ? '600px' : info.width,
                            init: function () {
                                $('#common-info-view').vm({
                                    attrs: info.data
                                })
                                $.each(info.data, function (k1, v1) {
                                    $.each(data, function (k2, v2) {
                                        if (v1.field == k2) {
                                            let id = '#info-view-' + k2;
                                            if (typeof v1.templet == 'function') {
                                                $(id).html(v1.templet(v2));
                                            } else {
                                                $(id).html(v2);
                                            }
                                        }
                                    })
                                });
                                //标注选中样式
                                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                            }
                        });
                    });
                }
                // 表格右侧按钮
                if (tool) {
                    let rowToolbar = 'tool(' + table_id + ')';
                    table.on(rowToolbar, tool);
                }
                // 表格顶部按钮
                if (bar) {
                    let rowToolbar = 'toolbar(' + table_id + ')';
                    table.on(rowToolbar, bar);
                }
                // 表格单选监听
                if (radio) {
                    let rowRadio = 'radio(' + table_id + ')';
                    table.on(rowRadio, radio);
                }
                // 单选获取选中行事件
                if (click) {
                    let rowClick = 'row(' + table_id + ')';
                    table.on(rowClick, click);
                }
                return baseTable;
            },
            check: function (_id) {
                return table.checkStatus(_id);
            },
            reload: function (_id) {
                table.reload(_id, {});
            }
        },
        string: {
            time: function (_t) {
                let t = parseInt(_t);
                if(t < 10){
                    t = '0' + t;
                }
                return t;
            }
        },
        convert: {
            s2n: function (_s){
                let n = 0;
                if(!isNaN(parseInt(_s))){
                    n = parseInt(_s);
                }
                return n;
            },
            t2d: function (_t) {
                return new Date(parseInt(_t) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
            },
            d2t: function (_t) {
                var timestamp = Date.parse(new Date(_t));
                return timestamp;
            },
            timestamp2time: function (_timestamp) {
                if (_timestamp > 0) {
                    let d = new Date(_timestamp * 1000);    // 根据时间戳生成的时间对象
                    let date = (d.getFullYear()) + "-" +
                        kit.string.time((d.getMonth() + 1)) + "-" +
                        kit.string.time(d.getDate()) + " " +
                        kit.string.time(d.getHours()) + ":" +
                        kit.string.time(d.getMinutes()) + ":" +
                        kit.string.time(d.getSeconds());
                    return date;
                } else {
                    return '';
                }
            },
            timestamp2shortdate: function (_date) {
                var date = new Date(parseInt(_date) * 1000);
                var short_date = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
                return short_date;
            },
            formatFormData: function (popType, list, _data, formatType) {
                switch (popType) {
                    case 2:
                        for (let i = 0, len = list.length; i < len; i++) {
                            for (let j = 0, len2 = list[i].length; j < len2; j++) {
                                let obj = list[i][j]
                                formatDate(obj, _data)
                            }
                        }
                        break;
                    case 3:
                        for (let i = 0, len = list.length; i < len; i++) {
                            let tabs = list[i].tab
                            for (let j = 0, len2 = tabs.length; j < len2; j++) {
                                let obj = tabs[j]
                                formatDate(obj, _data)
                            }
                        }
                        break;
                    default:
                        for (let i = 0, len = list.length; i < len; i++) {
                            let obj = list[i]
                            formatDate(obj, _data)
                        }
                        break;
                }
                function formatDate(obj, _data) {
                    if (formatType)//数据提交的时候格式数据
                    {
                        if (obj.code == 4) {
                            if (_data[obj.field]) {
                                _data[obj.field] = soda.convert.d2t(_data[obj.field]) / 1000
                            }
                        }
                        else if (obj.code == 7) {
                            _data[obj.field] = kit.form.edit.get(obj.editIndex);
                        }
                        else if (obj.code == 8) {
                            var emp = $("#" + obj.field).parent().find('img')[0]
                            let tem = $(emp).attr('src').split('?');
                            _data[obj.field] = tem[0];
                        }
                        else if (obj.code == 9) {
                            var imgs = $("#" + obj.field).parent().find('.layui-upload-list img')
                            var imgArr = [];
                            for (var i = 0, len = imgs.length; i < len; i++) {
                                let tem = $(imgs[i]).attr("src").split('?');
                                imgArr.push(tem[0])
                            }
                            _data[obj.field] = imgArr.join(',');
                        }
                        else if (obj.code == 10) {
                            var links = $("#" + obj.field).parent().find('.layui-upload-list a')
                            var linkArr = [];
                            for (var i = 0, len = links.length; i < len; i++) {
                                linkArr.push($(links[i]).attr("href"))
                            }
                            _data[obj.field] = linkArr.join(',');
                        }
                        else if (obj.code == 12) {
                            _data[obj.field] = soda.form.getSingItemValue(obj.code, obj.field);
                        }
                        else if (obj.code == 16) {
                            var codes = [];
                            var list = obj.empHander.selected
                            for (var i = 0; i < list.length; i++) {
                                codes.push(list[i].id)
                            }
                            _data[obj.field] = codes.join(',')
                        }
                    }
                    else {
                        if (obj.code == 4) {
                            laydate.render({
                                elem: '#' + obj.field
                            });
                            if(parseInt(_data[obj.field]) > 10000){
                                _data[obj.field] = soda.convert.timestamp2shortdate(_data[obj.field])
                            }
                        }
                        else if (obj.code == 7) {
                            obj.editIndex = kit.form.edit.init(obj.field, obj.config);
                            if (_data && _data[obj.field]) {
                                kit.form.edit.set(obj.editIndex, _data[obj.field]);
                            }
                        }
                        else if (obj.code == 8) {
                            var emp = $("#" + obj.field).parent().find('img')
                            common_upload.singular(function (data) {//传递函数作为参数，用于获取异步上传后返回的数据
                                $(emp).attr('src', data.res.requestUrls[0]);
                            }, {id: obj.field});
                            if (_data && _data[obj.field]) {
                                $(emp).attr('src', _data[obj.field]);
                            }
                        }
                        else if (obj.code == 9) {
                            initCode9(obj, _data);
                        }
                        else if (obj.code == 10) {
                            initCode10(obj, _data);
                        }
                        else if (obj.code == 12) {
                            if (_data && _data[obj.field]) {
                                var itemValue = _data[obj.field];
                                if (Array.isArray(itemValue)) {
                                    var str = '';
                                    for (var i = 0, len = itemValue.length; i < len; i++) {
                                        var key = itemValue[i].key;
                                        var value = itemValue[i].val;
                                        str = str + ' <div class="line">' +
                                            '     <div class="item" data-type="key">' + key + '</div>' +
                                            '     <div class="item" data-type="val">' + value + '</div>' +
                                            '     <div class="item" style="color:red;cursor:pointer;" onclick="javascript:$($(this).parent()).remove();">删除</div>' +
                                            ' </div>'
                                    }
                                    var node = $("#" + obj.field).find('.content')[0]
                                    $(node).append(str)
                                }
                            }
                        }
                        else if (obj.code == 16) {
                            var kList = [];
                            for (var i = 0; i < obj.items.length; i++) {
                                kList.push({
                                    id: obj.items[i].value,
                                    name: obj.items[i].title
                                })
                            }
                            if (_data && _data[obj.field]) {
                                obj.empHander = selectM({
                                    elem: '#' + obj.field,
                                    data: kList,
                                    max: 100,
                                    verify: 'required',
                                    selected: _data[obj.field].split(',')
                                });
                            } else {
                                obj.empHander = selectM({
                                    elem: '#' + obj.field,
                                    data: kList,
                                    max: 100,
                                    verify: 'required'
                                });
                            }

                        }
                    }
                }
                function initCode9(obj, _data) {
                    var emp = $("#" + obj.field).parent().find('.layui-upload-list')[0]
                    common_upload.more(function (data) {
                        var file_name = data.name.split('/')[1];
                        $(emp).append('<div style="position: relative;"><img src="' + data.res.requestUrls[0] + '?x-oss-process=style/img-baby-min-preview" alt="' + file_name + '" class="layui-upload-img" style="width:auto;max-height: 100px;margin: 10px 14px 0 0;"><i class="layui-icon layui-icon-close" onclick="delete_img(\'' + file_name + '\',this)" style="width:16px;height:16px;line-height:16px;cursor: pointer;position: absolute;top:-4px;right:1px;border: 1px solid #7d7d7d;border-radius: 50%;"></i></div>')
                    }, {id: obj.field});
                    if (_data && _data[obj.field]) {
                        var arrList = _data[obj.field].split(','), str = ''
                        for (var i = 0, len = arrList.length; i < len; i++) {
                            var file_name = arrList[i].substr(arrList[i].lastIndexOf('/') + 1, arrList[i].length)
                            str = str + '<div style="position: relative;"><img src="' + arrList[i] + '?x-oss-process=style/img-baby-min-preview" alt="' + file_name + '" class="layui-upload-img" style="width:auto;max-height: 100px;margin: 10px 14px 0 0;"><i class="layui-icon layui-icon-close" onclick="delete_img(\'' + file_name + '\',this)" style="width:16px;height:16px;line-height:16px;cursor: pointer;position: absolute;top:-4px;right:1px;border: 1px solid #7d7d7d;border-radius: 50%;"></i></div>'
                        }
                        $(emp).append(str)
                    }
                }
                function initCode10(obj, _data) {
                    var emp = $("#" + obj.field).parent().find('.layui-upload-list')[0]
                    common_upload.file(function (data) {
                        var file_name = data.name.split('/')[1];
                        $(emp).append('<div style="width:100%;position: relative;">  <span><a href="' + data.res.requestUrls[0] + '" title="点击下载" style="color:#0fb0e5">' + file_name + '</a><i class="layui-icon layui-icon-close" onclick="javascript:$($(this).parent().parent()).remove();" style="position: absolute;margin: 2px 0 0 20px;cursor: pointer;"></i></span></div>')
                    }, {id: obj.field});
                    if (_data && _data[obj.field]) {
                        var list = _data[obj.field].split(','), str = '';
                        for (var i = 0, len = list.length; i < len; i++) {
                            var file_name = list[i].substr(list[i].lastIndexOf('/') + 1, list[i].length)
                            str = str + '<div style="width:100%;position: relative;">  <span><a href="' + list[i] + '" title="点击下载" style="color:#0fb0e5">' + file_name + '</a><i class="layui-icon layui-icon-close" onclick="javascript:$($(this).parent().parent()).remove();" style="position: absolute;margin: 2px 0 0 20px;cursor: pointer;"></i></span></div>'
                        }
                        $(emp).append(str)
                    }
                }
            }
        },
        random: {
            number: function (_len) {
                let num = "";
                for(let i=0; i<_len; i++){
                    num += Math.floor(Math.random() * 10);
                }
                return num;
            },
            str: function (_len) {
                let str = "",
                    arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

                for(let i=0; i< _len; i++){
                    let pos = Math.round(Math.random() * (arr.length-1));
                    str += arr[pos];
                }
                return str;
            }
        },
        search: {
            init: function(_obj){
                let html = '';
                for(let i=0;i<_obj.length;i++){
                    html += kit.search.component(_obj[i]);
                }
                html = '<div class="layui-form-item" style="margin-bottom: 0px;">' + html + '</div>'
                $('#content-header-query-form').html(html);
                for(let i=0;i<_obj.length;i++){
                    if(_obj[i].type == 'date'){
                        laydate.render({
                            elem: '#query_' + _obj[i].field
                        });
                    }
                }
                form.render();
            },
            component: function (_obj) {
                let html = '';

                switch (parseInt(_obj.code)) {
                    case 1:         // 输入 text/number...
                        let date = _obj.type == 'date' ? ' lay-verify="date" ' : '';
                        let type = _obj.type == 'date' ? 'text' : _obj.type;
                        html = '<div class="layui-inline">\n' +
                            '                    <label class="layui-form-label">' + _obj.title + '</label>\n' +
                            '                    <div class="layui-input-inline">\n' +
                            '                        <input ' + date + ' placeholder="' + _obj.placeholder + '" type="' + type + '" name="' + _obj.field + '" id="query_' + _obj.field + '" autocomplete="off" class="layui-input">\n' +
                            '                    </div>\n' +
                            '                </div>';
                        break;
                    case 2:         // select
                        let option = '';
                        for(let i=0;i<_obj.items.length;i++){
                            option += '<option value="' + _obj.items[i].value + '">' + _obj.items[i].title + '</option>'
                        }
                        html += '<div class="layui-inline">\n' +
                            '                    <label class="layui-form-label">' + _obj.title + '</label>\n' +
                            '                    <div class="layui-input-inline">\n' +
                            '                        <select name="' + _obj.field + '">\n' +
                            '<option value="0">--请选择--</option>'+
                            option +
                            '                        </select>\n' +
                            '                    </div>\n' +
                            '                </div>';
                        break;
                }
                return html;
            },
            form: function () {
                return soda.form.get('#content-header-query-form');
            }
        }
    }

    // ajax请求头
    var header = {
        "Content-Type": "application/json;charset=UTF-8",
        "token": (typeof kit.session.getToken()) == 'undefined' ? '' : kit.session.getToken(),
    };
    // ajax请求主函数
    var ajax = {
        send: function (request) {
            if (request.token) {
                header.token = request.token;
            }
            if((typeof request.tips) == "undefined"){
                request.tips = 1;
            }
            $.ajax({
                type: request.method,
                url: action + request.url,
                data: ajax.empty(request.data),
                async: typeof(request.async) == 'undefined' ? 'true' : request.async,
                contentType: "application/json;charset=UTF-8",
                cache: false,
                headers: header,
                dataType: 'JSON',
                beforeSend: function (xhr) {
                    if (typeof(request.cl) == 'undefined') {
                        kit.progress.open();
                    }
                },
               
                statusCode: {
                    200: function (d) {
                        kit.progress.close();
                    }
                },
                error: function (e) {
                    kit.progress.close();
                    request.err ? request.err() : '';
                    var res = e.responseJSON;
                    var status = e.status;
                    switch (status) {
                        case 401:
                            kit.tips.warning(res.message);
                            kit.session.clean();
                            setTimeout(function(){
                                window.location.href = '/login.html';
                            }, 2000);
                            break;
                        case 403:
                            kit.tips.warning(res.message);
                            break;
                        case 406:
                            kit.tips.warning(res.message);
                            break;
                        case 411:
                            kit.tips.warning(res.message);
                            break;
                        case 429:
                            kit.tips.warning(res.message);
                            break;
                        default:
                            try {
                                kit.tips.warning(res.message);
                            } catch (e) {
                                kit.tips.warning('服务器异常通讯失败');
                            }
                    }

                },
                success: function(d){
                    kit.progress.close();
                    if(request.tips == 1){
                        kit.tips.ok(d.message);
                    }
                    if(request.isAll){
                        request.callback ? request.callback(d) : '';
                    }else{
                        request.callback ? request.callback(d.result) : '';
                    }
                }
            });
        },
        empty: function (value) {
            if (typeof(value) == "undefined")
                return "";
            if (typeof(value) == "object")
                return JSON.stringify(value);
            return value;
        }
    }
    exports('kit', kit);
});

